<?php
//https://www.goodreads.com/search.xml?key=YOUR_KEY&q=Ender%27s+Game
//Not request any method more than once a second. Goodreads tracks all requests made by developers.

include 'functions.php';
require 'secure.php';

//for books only

	//starting information
	$rootTitle = stripslashes($rootTitle);

	//parse determiners from title and put them back at the front
	//The
	if (stripos($rootTitle, ', the')>=1)
	{
		$rootTitle = str_ireplace(', the', '', $rootTitle);
		$rootTitle = 'the '.$rootTitle;
	}
	//An
	if (stripos($rootTitle, ', an')>=1)
	{
		$rootTitle = str_ireplace(', an', '', $rootTitle);
		$rootTitle = 'an '.$rootTitle;
	}
	//A
	if (stripos($rootTitle, ', a')>=1)
	{
		$rootTitle = str_ireplace(', a', '', $rootTitle);
		$rootTitle = 'a '.$rootTitle;
	}

	$titleGR = urlencode($rootTitle);
	$authorGR = urlencode($authorGR);


	//construct url
	$rooturl = 'https://www.goodreads.com/search.xml?key';
	$key = $grKey;
	$completeurl = "$rooturl=$key&q=$titleGR+$authorGR";

	//echo "URL: ".$completeurl."<br/><br/>";

	//construct curl request
	$request = curl_build($completeurl);
	//usleep(1100000);//delay for just over a second

	$xml = simplexml_load_string($request);//get file 

	//pub year
	$pubYearGR = $xml->search->results->work->original_publication_year;//find publication year
	$pubYearGR = explode("=>", $pubYearGR);
	$pubYearGR = $pubYearGR[0];

	//title
	$titleGRP = $xml->search->results->work->best_book->title;
	$titleGRP = explode("=>", $titleGRP);
	$titleGRP = $titleGRP[0];

	//author
	$authorGR = $xml->search->results->work->best_book->author->name;
	$authorGR = explode("=>", $authorGR);
	$authorGR = $authorGR[0];

	//image
	$imgUrlGR = $xml->search->results->work->best_book->image_url;
	$imgUrlGR = explode("=>", $imgUrlGR);
	$imgUrlGR = $imgUrlGR[0];

	$missingGR = $xml->search->{'total-results'};
	
	/*
	if ($missingGR > 0)
	{
		echo "<br/><br/>Goodreads API values:<br/>";
		echo "Author(s): ".$authorGR."<br/>";
		echo "Year: ".$pubYearGR."<br/>";
		echo "Title: ".$titleGRP."<br/>";
		
		echo "Original language: ".$languageP."<br/>";
		echo "Publisher: ".$pubP."<br/>";
		echo "ISBN: ".$ISBNP."<br/>";
		
		echo "<img src=$imgUrlGR height=250><br/>";
	}
	else
	{
		echo "Page missing!";
	}
	*/

?>